package com.uroad.jilinpda.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.uroad.jilinpda.R;
import com.uroad.lib.util.sys.SystemUtil;

/**
 * @创建者 wlw
 * @创建时间 2018/11/19 11:14
 * @描述
 */
public class TripDialog extends Dialog {
    private Context                context;
    private TextView               tvTitle;
    private TextView               tvContent;
    private Button                 btnConfirm;
    private Button                 btnCancel;
    //点击按钮后是否消失
    private boolean isDissmissAble = true;
    private OnDiglogClickListener onSuccessClickListener;

    public TripDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_trip, null);
        tvTitle = view.findViewById(R.id.tvTitle);
        tvContent = view.findViewById(R.id.tvContent);
        btnConfirm = view.findViewById(R.id.btnConfirm);
        btnCancel = view.findViewById(R.id.btnCancel);

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDissmissAble) {
                    dismiss();
                }
                if (onSuccessClickListener != null) {
                    onSuccessClickListener.onConfirm();
                }
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDissmissAble) {
                    dismiss();
                }
                if (onSuccessClickListener != null) {
                    onSuccessClickListener.onCancel();
                }
            }
        });

        setContentView(view);
        setCanceledOnTouchOutside(false);
        setCancelable(false);

        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        float width = SystemUtil.getScreenWidth((Activity) context);
        params.windowAnimations = (int) (width - 160);
        params.width = (int) (width - 160);
        params.gravity = Gravity.CENTER_HORIZONTAL;
        window.setAttributes(params);
        window.setBackgroundDrawable(new BitmapDrawable());
    }

    public void showCancelBtn(boolean isShow) {
        if (isShow) {
            btnCancel.setVisibility(View.VISIBLE);
        }else{
            btnCancel.setVisibility(View.GONE);
        }
    }

    public void setTitle(String title){
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    public void setContent(String content){
        if (tvContent != null) {
            tvContent.setText(content);
        }
    }

    public void setbtnCancel(String str){
        btnCancel.setText(str);
    }

    public void setbtnConfirm(String str){
        btnConfirm.setText(str);
    }

    public void setDismissAble(boolean isDissmissAble) {
        this.isDissmissAble = isDissmissAble;
    }

    public void setOnDialogClickListener(OnDiglogClickListener onSuccessClickListener) {
        this.onSuccessClickListener = onSuccessClickListener;
    }

    public interface OnDiglogClickListener{
        void onConfirm();
        void onCancel();
    }

}
